<?php


namespace App\Services;

class ExifService
{
    /**
     * 获取拍摄照片的元数据信息
     *
     * @param $file
     * @return array
     * @author: lxk
     * @date: 2024-09-20 11:18
     */
    public static function getExifInfo($file) : array
    {
        $exifInfo = [];
        $exif = exif_read_data($file->getRealPath(), 'EXIF');
        if (isset($exif['GPSLatitude']) && $exif['GPSLatitude']) {
            $lonRef    = $exif['GPSLongitudeRef']; // E (东经)
            $longitude = $exif['GPSLongitude']; //经度
            $latRef    = $exif['GPSLatitudeRef']; // N (北纬)
            $latitude  = $exif['GPSLatitude']; //纬度
            $lng       = round(self::convertGPStoDecimal($longitude, $lonRef), 6);
            $lat       = round(self::convertGPStoDecimal($latitude, $latRef), 6);
            $location  = "$lng,$lat";
            $address   = self::getDetailAddress($location);
            $make      = $exif['Make'];
            $model     = $exif['Model'];
            $dateTime  = $exif['DateTimeOriginal'];
            $fileSize  = $exif['FileSize'];
            $exifInfo  =  [
                'address'    => $address . '附近', //拍摄地址
                'make'       => $make, //手机品牌
                'model'      => $model, //手机型号
                'photo_time' => $dateTime, //拍摄时间
                'file_size'  => $fileSize, //照片大小
            ];
        }
        return $exifInfo;
    }

    /**
     * 根据经纬度，获取详细地址
     *
     * @param $location
     * @return mixed
     * @author: lxk
     * @date: 2024-09-20 10:58
     */
    public static function getDetailAddress($location)
    {
        $curl    = curl_init();
        $params  = ["location" => $location, "key" => config('site.map_key')]; //高德地图 Web服务 API ，坐标转换
        $query   = http_build_query($params);
        $fullUrl = "https://restapi.amap.com/v3/geocode/regeo" . '?' . $query;
        curl_setopt($curl, CURLOPT_URL, $fullUrl); // 设置URL
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 将cURL获取的内容赋值到变量
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// HTTPS请求时要注意绕过ssl验证
        $response = curl_exec($curl);
        if (empty($response)) {
            return '';
        } else {
            $response = json_decode($response, true);
        }
        curl_close($curl);

        return $response['regeocode']['formatted_address'];
    }

    /**
     * 辅助函数：将GPS坐标转换为十进制度数
     *
     * @param $coordinate
     * @param $ref
     * @return float|int|mixed
     * @author: lxk
     * @date: 2024-09-19 16:54
     */
    public static function convertGPStoDecimal($coordinate, $ref)
    {
        $degrees = count($coordinate) > 0 ? self::gps2num($coordinate[0]) : 0;
        $minutes = count($coordinate) > 1 ? self::gps2num($coordinate[1]) : 0;
        $seconds = count($coordinate) > 2 ? self::gps2num($coordinate[2]) : 0;

        $direction = ($ref == 'W' or $ref == 'S') ? -1 : 1;

        return $degrees + $minutes / 60 + $seconds / 3600 * $direction;
    }

    /**
     * 坐标数据处理
     *
     * @param $coordinate
     * @return float|int|mixed
     * @author: lxk
     * @date: 2024-09-19 16:54
     */
    public static function gps2num($coordinate)
    {
        $parts = explode('/', $coordinate);
        if (count($parts) <= 1)
            return $coordinate;

        return floatval($parts[0]) / floatval($parts[1]);
    }

}
